//
//  LZTStatusCell.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/13.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit
import SnapKit

class LZTStatusCell: UITableViewCell {
    
    //记录 底部试图的top约束
    var botomViewTopconstraint: Constraint?
    
    //通过viewModel获取数据 
    var statusViewModel: LZTStatusViewModel? {
        
        didSet{
            //原创微博的赋值
            originalView.statusViewModel = statusViewModel
            
            //转发微博
            
            //1.卸载约束
            botomViewTopconstraint?.uninstall()
            
            //nil代表没有没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                //底部试图以原创微博为基准
                bottomView.snp_updateConstraints(closure: { (make) in
                    //记录 约束以便于 下次执行时删除对应的约束
                    botomViewTopconstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                //隐藏转发微博
                retweetview.hidden = true
            } else {
                
                //有转发微博
                //转发微博的赋值
                retweetview.statusViewModel = statusViewModel
                
                //底部试图以转发微博为基准
                bottomView.snp_updateConstraints(closure: { (make) in
                    //记录 约束以便于 下次执行时删除对应的约束
                    botomViewTopconstraint = make.top.equalTo(retweetview.snp_bottom).constraint
                })
                //显示转发微博
                retweetview.hidden = false
            }
            
            //底部试图赋值
            bottomView.statusViewModel = statusViewModel
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
        
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetview)
        contentView.addSubview(bottomView)
        //添加约束
        originalView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(8)
            //该约束已经在 原创微博-LZTStatusOriginalView中进行设置。根据 微博正文label的内容+10设置
            //make.height.equalTo(50)
        }
        retweetview.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            //make.height.equalTo(50)
        }
        
        bottomView.snp_makeConstraints { (make) in
            //记录顶部约束
            botomViewTopconstraint = make.top.equalTo(retweetview.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //动态 设定cell的高度
        contentView.snp_makeConstraints { (make) in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
    }

    //懒加载 原创微博
    private lazy var originalView: LZTStatusOriginalView = LZTStatusOriginalView()
    //懒加载 转发微博
    private lazy var retweetview: LZTStatusRetweetview = LZTStatusRetweetview()
    
    //加载 底部试图
    private lazy var bottomView: LZTStatusBottomView = LZTStatusBottomView()
}
